package com.wxy.ej.flow.modules.act.listen;

import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.ExecutionListener;
import org.activiti.engine.delegate.Expression;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

/**
 * 任务调用前，根据规则编号，创建业务系统部门任务和人员，同时返回部门和人员信息。
 * 子流程调用了4次（4个机构）
 * 1001
 * [1001, 1002, 1003, 1004]
 */
@Slf4j
@Component("insertTaskSonListener")
@Scope("prototype")
public class InsertTaskSonListener implements ExecutionListener {


    private Expression ruleNumber;

    @Override
    public void notify(DelegateExecution delegateExecution) {
        log.info("---->act listener: InsertTaskSonListener");
        /*
        FlowElement flowElement = delegateExecution.getCurrentFlowElement();
        System.out.println("=== InsertTaskSonListener NodeName: "+flowElement.getName() +" Doc:"+ flowElement.getDocumentation());
        String flowElementName = flowElement.getName();

        String ruleNumberStr = null;
        if (StringUtils.isNull(ruleNumber)){
            ruleNumberStr = (String) delegateExecution.getVariable("MainTaskEventRuleNum");
        }
        else{
            ruleNumberStr = ruleNumber.getExpressionText();
        }
        String procInstId = delegateExecution.getProcessInstanceId();
        String taskIdBiz = delegateExecution.getProcessInstanceBusinessKey();

        System.out.println("EventName:" + delegateExecution.getEventName());
        System.out.println("ruleNumberStr:"+ruleNumberStr);
        System.out.println("procInstId:"+procInstId);
        System.out.println("taskIdBiz:"+taskIdBiz);

        // 添加部门子任务，和每个人的独立任务，返回信息
        Result result = cityTaskAutoService.insertCtcTaskSon(ruleNumberStr, taskIdBiz, flowElementName);
        System.out.println("result:"+result);


        // 流程执行日志
        // ${companyId}
        String doc = ExpressionUtils.parse(flowElement.getDocumentation(), delegateExecution);

        CtcTaskLogBizDto taskLogDto = new CtcTaskLogBizDto();
        taskLogDto.setCtcTaskId(Long.parseLong(delegateExecution.getProcessInstanceBusinessKey()));
        taskLogDto.setFlowElementId(flowElement.getId());
        taskLogDto.setFlowElementName(flowElement.getName());
        taskLogDto.setFlowElementDoc(doc);
        taskLogDto.setCreatedate(DateTool.currentDate("yyyy-MM-dd HH:mm:ss.SSS"));
        StringBuilder sb = new StringBuilder();
        sb.append("创建部门子任务和指派人员；");
        sb.append("是否成功："+result.isSuccess());
        taskLogDto.setDescription(sb.toString());
        ctcTaskLogBizService.create(taskLogDto);*/
    }


}
